var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var rootview = ui("$");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var openHelper = require("helper/open");
var mallUrl = require("url/mall");
var deviceUtil = require("helper/device");
var communityUtil = require("helper/community");
var layoutRoot = ui("layoutRoot");
var listview = ui("ListView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据
var noData = [{"template":4,"labellContent":"暂时没有商品"}];

//当前页码
var pageNum = 1;
//是否最后一页

//andriod返回键
page.on("back", function() {
	deviceUtil.home();
});

//从新增页面返回,刷新数据
page.on("result", function(data){
	if(data && data.cart_main){
		getCategoryProduct(true);
	}
});

/****************处理页面-头部-begin*****************/
var do_ALayout_header = ui("do_ALayout_header");
var mainHeadAdd = do_ALayout_header.add("mainHead", "source://view/reuse/header/mallHeader.ui", 0, 0);
var mainHead = ui("mainHead");
var cartNumLayout = ui(mainHeadAdd+".cartNumLayout");
var cartNum = ui(mainHeadAdd+".cartNum");

init();

function init(){
	getCategoryProduct(true);
}
/****************处理页面-头部-end*****************/


function getCategoryProduct(flag){
	http.post(mallUrl.url.product.index,{communityId:communityUtil.communityVisitId()},function(data){		
		cartNum.text = data.cartTotalProduct;
		if(cartNum.text > 0) {
			cartNumLayout.visible = true;
		}else{
			cartNumLayout.visible = false;
		}
		listdata.removeAll();
		//轮播广告
		if(data.bannerList){
			listdata.addData([{"template": 1,"bannerList":data.bannerList}]);
		}
		//便利店
		/*if(data.qqStore && data.qqStoreProducts && data.qqStoreProducts.length>0){
			listdata.addData([{"template": 7,"shopId": data.qqStore.id,"imgLogo":"source://image/mall/logo.png","shopnavImg":data.qqStoreProducts[0].img}]);
		}*/
		//商品分类
		if(data.categoryList){
			categoryList = data.categoryList;
			if(categoryList.length > 0){
				var cateBgs = ["ff7c24FF","808cfcff","f9cc00ff","97df02ff","f25780ff","00c973ff","b27feaff","34b5feFF","f35656FF","f25780FF"];
				for(var i in categoryList){
					categoryList[i].cateBg = cateBgs[i];
				}
				var allCategory = {cateBg:'8053dcff',categoryId:'0',name:"更多分类",pathImg:"source://image/mall/more.png"};//更多圈子
				categoryList.push(allCategory);
				listdata.addData([{"template": 6,"categoryList":categoryList}]);
			}
		}
		//活动专区
		if(data.subjectList){
			listdata.addData([{"template": 5,"gbItems":data.subjectList}]);
		}
		//外部商家
		var esData = data.externalSpList;
		if(esData){
			var gvTitle01='',gvSubTitle01='',gvImg01='',gvId01,
			gvTitle02='',gvSubTitle02='',gvImg02='',gvId02,
			gvTitle03='',gvSubTitle03='',gvImg03='',gvId03,
			gvTitle04='',gvSubTitle04='',gvImg04='',gvId04,
			gvTitle05='',gvSubTitle05='',gvImg05='',gvId05;
			if(esData.length>0){
				gvId01 = esData[0].id;
				gvTitle01 = esData[0].shortName;
				gvSubTitle01 = esData[0].subtitle;
				gvImg01 = esData[0].fileDomain+esData[0].firstImg;
			}
			if(esData.length>1){
				gvId02 = esData[1].id;
				gvTitle02 = esData[1].shortName;
				gvSubTitle02 = esData[1].subtitle;
				gvImg02 = esData[1].pathFirstImg;
			}
			if(esData.length>2){
				gvId03 = esData[2].id;
				gvTitle03 = esData[2].shortName;
				gvSubTitle03 = esData[2].subtitle;
				gvImg03 = esData[2].pathFirstImg;
			}
			if(esData.length>3){
				gvId04 = esData[3].id;
				gvTitle04 = esData[3].shortName;
				gvSubTitle04 = esData[3].subtitle;
				gvImg04 = esData[3].pathFirstImg;
			}
			if(esData.length>4){
				gvId05 = esData[4].id;
				gvTitle05 = esData[4].shortName;
				gvSubTitle05 = esData[4].subtitle;
				gvImg05 = esData[4].pathFirstImg;
			}
			listdata.addData([{"template":4,"gvId01":gvId01,"gvTitle01":gvTitle01,"gvSubTitle01":gvSubTitle01,"gvImg01":gvImg01,"gvId02":gvId02,"gvTitle02":gvTitle02,"gvSubTitle02":gvSubTitle02,"gvImg02":gvImg02,"gvId03":gvId03,"gvTitle03":gvTitle03,"gvSubTitle03":gvSubTitle03,"gvImg03":gvImg03,"gvId04":gvId04,"gvTitle04":gvTitle04,"gvSubTitle04":gvSubTitle04,"gvImg04":gvImg04,"gvTitle05":gvTitle05,"gvSubTitle05":gvSubTitle05,"gvImg05":gvImg05,"gvId05":gvId05}]);
		}
		
		//分类商品列表
		var categoryProductList = data.list;
		if(categoryProductList && categoryProductList.length > 0){
			var categoryList2 = [];
			for(var i in categoryProductList){
				categoryProductList[i].template = 0;//指定模板
				if(categoryProductList[i].productList != null && categoryProductList[i].productList.length > 0){
					categoryList2.push(categoryProductList[i]);//新的
				}
			}
			listdata.addData(categoryList2);
		}
		
		listview.refreshItems();//刷新item数据
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		getCategoryProduct(false);
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		//getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});
//响应点击主页底部【商城】刷新本页面动态数据
page.on("index_mall_tab_touch", function(data) {
	init();
});